<template>
  <div class="page flex-center">
    <div class="sign-box">
      <h1 class="title">登录</h1>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="86px">
        <el-form-item label="用户名" prop="account">
          <el-input v-model="form.account" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="w100p" :loading="loading" @click="doLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { login } from '@/apis/login'

const router = useRouter()
// const { t } = useI18n()
const store = useStore()

const formRef = ref()
const loading = ref(false)
const form = reactive({
  account: 'admin',
  password: 'admin'
})

const doLogin = () => {
  formRef.value.validate((valid) => {
    console.log('valid', valid)
    if (!valid) {
      return
    }
    loading.value = true
    console.log('哈哈哈哈哈哈哈哈哈哈哈')
    login(form)
      .then((res) => {
        console.log('res', res)
        store.commit('user/setToken', res.data.token)

        store.dispatch('user/refreshInfo')
        // 设置路由/菜单还没有加载
        store.commit('setRouteLoaded', false)

        router.push('/')
      })
      .finally(() => {
        loading.value = false
      })
  })
}

const rules = reactive({
  account: [
    { required: true, message: '请输入你的sb用户名', trigger: ['change', 'blur'] },
    { pattern: /^[a-zA-Z][a-zA-Z0-9_-]{3,31}$/, message: '3-31位长度', trigger: ['change', 'blur'] }
  ],
  password: [{ required: true, min: 4, message: '请输入你的sb密码', trigger: ['change', 'blur'] }]
})
</script>

<style scoped lang="scss">
.page {
  height: 100%;
  // background: url(@/assets/bg.jpg) no-repeat;
  background-size: cover;
}

.sign-box {
  width: 400px;
  background: #fff;
  padding: 30px 50px 20px 30px;
  border-radius: 4px;
  box-shadow: 0 0 10px #022c44;
}

.title {
  text-align: center;
  font-size: 20px;
  line-height: 30px;
  margin-top: 0;
  margin-bottom: 10px;
  color: #000;
}
</style>
